import { Notification } from 'element-ui';

export const handledTip =(message = "成功", type = 'success', d = 700) =>{
	Notification({
		title: "",
		message,
		type,
		duration: d
	})
}

export const handledMsgCopy=(msg) =>{
	// 将文本复制到剪贴板
	if (navigator.clipboard && window.isSecureContext) {
		navigator.clipboard.writeText(msg)
			.then(() => {
				handledTip("复制成功")
			})
			.catch(() => {
				handledTip("复制失败", "info")
			})
	} else {
		// 创建text area
		const textArea = document.createElement('textarea')
		textArea.value = msg
		// 使text area不在viewport，同时设置不可见
		document.body.appendChild(textArea)
		textArea.focus()
		textArea.select()
		return new Promise((resolve, reject) => {
			// 执行复制命令并移除文本框
			document.execCommand('copy') ? resolve() : reject(new Error('出错了'))
			textArea.remove()
		}).then(
			() => {
				handledTip("复制成功")
			},
			() => {
				handledTip("复制失败", "info")
			}
		)
	}
}